# coding=utf-8
# -*- coding: UTF-8 -*-
#
# This file is part of the kernelstudio package.
#
# (c) 2014-2023 zlin <admin@kernelstudio.com>
#
# For the full copyright and license information, please view the LICENSE file
# that was distributed with this source code.

import time
import typing as tp


def year(t=time.localtime()) -> int:
    return t.tm_year


def month(t=time.localtime()) -> int:
    return t.tm_mon


def day(t=time.localtime()) -> int:
    return t.tm_mday


def hour(t=time.localtime()) -> int:
    return t.tm_hour


def minute(t=time.localtime()) -> int:
    return t.tm_min


def second(t=time.localtime()) -> int:
    return t.tm_sec


def to_path(t=time.localtime()) -> str:
    return str(t.tm_year) + "/" + str(t.tm_mon) + "/" + str(t.tm_mday)


def to_long(t=time.localtime()) -> str:
    return str(t.tm_year) + str(t.tm_mon) + str(t.tm_mday) + str(t.tm_hour) + str(t.tm_min) + str(t.tm_sec)


def chinese(t=time.localtime()) -> str:
    y, mo, d, h, m, s = [t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec]
    times = str(y) + '年' + str(mo) + '月' + str(d) + '日' + str(h) + '时' + str(m) + '分' + str(s) + "秒"
    return times


def timestamp() -> int:
    return int(time.time() * 1000)


def timestamp_seconds() -> int:
    return int(time.time())


def year_month_day(t=time.localtime()) -> str:
    y, mo, d, h, m, s = [t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec]
    return str(y) + '-' + str(mo) + '-' + str(d)


def now(t=time.localtime()) -> str:
    y, mo, d, h, m, s = [t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec]
    times = str(y) + '-' + str(mo) + '-' + str(d) + ' ' + str(h) + ':' + str(m) + ':' + str(s)
    return times


def parse_timestamp_seconds(s: tp.Optional[str], defaults: tp.Optional[int] = 0) -> int:
    if s is None:
        return defaults
    s = s.lower()
    if s.endswith("y"):
        # 年
        return int(s.split("y")[0]) * 365 * 24 * 60 * 60
    elif s.endswith("m"):
        # 月
        return int(s.split("m")[0]) * 30 * 24 * 60 * 60
    elif s.endswith("d"):
        # 天
        return int(s.split("d")[0]) * 24 * 60 * 60
    elif s.endswith("h"):
        # 小时
        return int(s.split("h")[0]) * 60 * 60
    elif s.endswith("f"):
        # 分钟
        return int(s.split("f")[0]) * 60
    elif s.endswith("s"):
        # 分钟
        return int(s.split("s")[0])
    return defaults


def parse_timestamp(s: tp.Optional[str]) -> int:
    return parse_timestamp_seconds(s) * 1000
